package com.ruoyi.jiajia.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.JiajiaWorker;
import com.ruoyi.system.domain.JiajiaWorkerRenzheng;
import com.ruoyi.system.service.IJiajiaWorkerRenzhengService;
import com.ruoyi.system.service.IJiajiaWorkerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 身份认证Controller
 * 
 * @author ruoyi
 * @date 2021-10-13
 */
@Controller
@RequestMapping("/jiajia/renZheng")
public class JiajiaWorkerRenzhengController extends BaseController
{
    private String prefix = "jiajia/renZheng";

    @Autowired
    private IJiajiaWorkerRenzhengService jiajiaWorkerRenzhengService;

    @Autowired
    private IJiajiaWorkerService workerService;

    @RequiresPermissions("jiajia:renZheng:view")
    @GetMapping()
    public String renZheng()
    {
        return prefix + "/renZheng";
    }

    /**
     * 查询身份认证列表
     */
    @RequiresPermissions("jiajia:renZheng:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JiajiaWorkerRenzheng jiajiaWorkerRenzheng)
    {
        startPage();
        List<JiajiaWorkerRenzheng> list = jiajiaWorkerRenzhengService.selectJiajiaWorkerRenzhengList(jiajiaWorkerRenzheng);
        return getDataTable(list);
    }

    /**
     * 导出身份认证列表
     */
    @RequiresPermissions("jiajia:renZheng:export")
    @Log(title = "身份认证", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JiajiaWorkerRenzheng jiajiaWorkerRenzheng)
    {
        List<JiajiaWorkerRenzheng> list = jiajiaWorkerRenzhengService.selectJiajiaWorkerRenzhengList(jiajiaWorkerRenzheng);
        ExcelUtil<JiajiaWorkerRenzheng> util = new ExcelUtil<JiajiaWorkerRenzheng>(JiajiaWorkerRenzheng.class);
        return util.exportExcel(list, "身份认证数据");
    }

    /**
     * 新增身份认证
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存身份认证
     */
    @RequiresPermissions("jiajia:renZheng:add")
    @Log(title = "身份认证", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JiajiaWorkerRenzheng jiajiaWorkerRenzheng)
    {
        return toAjax(jiajiaWorkerRenzhengService.insertJiajiaWorkerRenzheng(jiajiaWorkerRenzheng));
    }

    /**
     * 修改身份认证
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        JiajiaWorkerRenzheng jiajiaWorkerRenzheng = jiajiaWorkerRenzhengService.selectJiajiaWorkerRenzhengById(id);
        mmap.put("jiajiaWorkerRenzheng", jiajiaWorkerRenzheng);
        return prefix + "/edit";
    }


    /**
     * 修改保存身份认证
     */
    @RequiresPermissions("jiajia:renZheng:edit")
    @Log(title = "身份认证", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JiajiaWorkerRenzheng jiajiaWorkerRenzheng)
    {
        return toAjax(jiajiaWorkerRenzhengService.updateJiajiaWorkerRenzheng(jiajiaWorkerRenzheng));
    }

    /**
     * 审核通过
     */
    @RequiresPermissions("jiajia:renZheng:edit")
    @PostMapping("/shenHeTongGuo")
    @ResponseBody
    public AjaxResult shenHeTongGuo(JiajiaWorkerRenzheng jiajiaWorkerRenzheng)
    {
        JiajiaWorkerRenzheng searchRenZheng = jiajiaWorkerRenzhengService.selectJiajiaWorkerRenzhengById(jiajiaWorkerRenzheng.getId());
        if(searchRenZheng == null)return AjaxResult.refreshAgain();
        JiajiaWorker worker = workerService.selectJiajiaWorkerById(searchRenZheng.getWid());
        if (worker == null)return AjaxResult.refreshAgain();
        if (jiajiaWorkerRenzheng.getAuthStatus().intValue() == 1){
            worker.setCodeAuth(1);
            worker.setStatus(0);//默认休息中
            workerService.updateJiajiaWorker(worker);
        }

        return toAjax(jiajiaWorkerRenzhengService.updateJiajiaWorkerRenzheng(jiajiaWorkerRenzheng));
    }


    /**
     * 删除身份认证
     */
    @RequiresPermissions("jiajia:renZheng:remove")
    @Log(title = "身份认证", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jiajiaWorkerRenzhengService.deleteJiajiaWorkerRenzhengByIds(ids));
    }


    /**
     * 打款
     */
    @RequiresPermissions("jiajia:renZheng:makePayment")
    @Log(title = "身份认证", businessType = BusinessType.OTHER)
    @PostMapping( "/makePayment")
    @ResponseBody
    public AjaxResult makePayment()
    {jiajiaWorkerRenzhengService.makePayment();
        return AjaxResult.success();
    }
}
